//
//  XQIdentityPopSubCell.m
//  JoinPeso
//
//  Created by 小琦 on 2024/12/2.
//

#import "XQIdentityPopSubCell.h"

@interface XQIdentityPopSubCell()
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIImageView *arrowImageView;
@property (nonatomic,strong) UIButton *button;
@end

@implementation XQIdentityPopSubCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self.contentView addSubview:self.titleLb];
    [self.contentView addSubview:self.textField];
    [self.textField addSubview:self.arrowImageView];
    [self.textField addSubview:self.button];
}

- (void)makeConstraints {
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@20);
        make.height.mas_equalTo(22);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb.mas_bottom).offset(10);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.height.mas_equalTo(35);
    }];
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerY;
        make.right.equalTo(@-10);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
}

#pragma mark - setter

- (void)setTitle:(NSString *)title {
    self.titleLb.text = title;
}

- (void)setContent:(NSString *)content {
    self.textField.text = content;
}

- (void)setShowArrow:(BOOL)showArrow {
    self.arrowImageView.hidden = !showArrow;
    self.button.hidden = !showArrow;
}

#pragma mark - private

- (void)buttonClick {
    if (self.buttonHandle) {
        self.buttonHandle();
    }
}

// 监听文本变化的方法
- (void)textFieldDidChange:(UITextField *)textField {
    if (self.textFielHandle) {
        self.textFielHandle(textField.text);
    }
}

#pragma mark - lazy

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.textColor = kBlackTextColor;
        _titleLb.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    }
    return _titleLb;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.backgroundColor = [UIColor colorWithHexString:@"#F7F7F9"];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
        _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textField.layer.cornerRadius = 8;
        _textField.textColor = kBlackTextColor;
        _textField.keyboardType = UIKeyboardTypeASCIICapable;
        [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        CGRect frame = _textField.frame;
        frame.size.width = 10;
        UIView *leftview = [[UIView alloc] initWithFrame:frame];
        leftview.backgroundColor = [UIColor clearColor];
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.leftView = leftview;
    }
    return _textField;
}

- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
        _arrowImageView.image = [UIImage imageNamed:@"identity_info_arrow"];
        _arrowImageView.hidden = YES;
    }
    return _arrowImageView;
}

- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.hidden = YES;
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}


@end
